package com.cqndt.disaster.device.service.impl;

import java.util.Map;
import java.util.List;
import java.util.HashMap;
import java.util.ArrayList;
import java.math.BigDecimal;
import com.cqndt.disaster.device.dao.*;
import org.springframework.util.StringUtils;
import com.cqndt.disaster.device.vo.SearchVo;
import org.springframework.stereotype.Service;
import com.cqndt.disaster.device.util.DateFormatUtil;
import com.cqndt.disaster.device.service.AppTabDeviceService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author lgb
 * @date 2020-9-1
 * @return 
 **/
@Service
public class AppTabDeviceServiceImpl implements AppTabDeviceService {
    @Autowired
    private AppTabDeviceMapper appTabDeviceMapper;
    @Autowired
    private TabSensorInitMapper tabSensorInitMapper;
    @Autowired
    private AppTabStaticTypeMapper appTabStaticTypeMapper;
    @Override
    public Integer selectCountDevice(String userId) {
        return appTabDeviceMapper.selectCountDevice(userId);
    }
    @Override
    public Integer selectOnlineCountDevice(String userId) {
        List<Map<String, Object>> list = appTabStaticTypeMapper.selectByStaticNum("11");
        //离线时间判断
        Integer num = (Integer) list.get(0).get("id");
        Integer zxNum = appTabDeviceMapper.countDeviceStateZx(userId, num);
        return zxNum;
    }
    @Override
    public Integer selectOfflineCountDevice(String userId) {
        List<Map<String, Object>> list = appTabStaticTypeMapper.selectByStaticNum("11");
        //离线时间判断
        Integer num = (Integer) list.get(0).get("id");
        Integer lxNum = appTabDeviceMapper.countDeviceStateLx(userId, num);
        return lxNum;
    }
    @Override
    public List<Map<String, Object>> selectOnlineRateByProject(String userId, String seachText) {
        List<Map<String, Object>> list = appTabStaticTypeMapper.selectByStaticNum("11");
        //离线时间判断
        Integer num = (Integer) list.get(0).get("id");
        return appTabDeviceMapper.selectOnlineRateByProject(userId, num, seachText);
    }
    @Override
    public List<Map<String, Object>> selectOnlineDeviceByProjectID(Integer projectID) {
        List<Map<String, Object>> list = appTabStaticTypeMapper.selectByStaticNum("11");
        //离线时间判断
        Integer num = (Integer) list.get(0).get("id");
        return appTabDeviceMapper.selectOnlineDeviceByProjectID(projectID, num);
    }
    @Override
    public List<Map<String, Object>> selectPerson(Integer projectID) {
        return appTabDeviceMapper.selectPerson(projectID);
    }
    @Override
    public Map<String, Object> selectDeviceById(int deviceId) {
        List<Map<String, Object>> list = appTabStaticTypeMapper.selectByStaticNum("11");
        //离线时间判断
        Integer num = (Integer) list.get(0).get("id");
        return appTabDeviceMapper.selectDeviceById(deviceId, num);
    }
    @Override
    public List<Map<String, Object>> deviceCoordinateByProjectId(int id) {
        return appTabDeviceMapper.deviceCoordinateByProjectId(id);
    }
    @Override
    public List<Map<String, Object>> listTabSensor(String deviceId) {
        return appTabDeviceMapper.listTabSensor(deviceId);
    }
    @Override
    public List<Map<String, Object>> selectDeviceTypeStatistics(String userId) {
        return appTabDeviceMapper.selectDeviceTypeStatistics(userId);
    }
    @Override
    public Integer selectDeviceTypeStatisticsTotal(String userId) {
        return appTabDeviceMapper.selectDeviceTypeStatisticsTotal(userId);
    }
    @Override
    public List<Map<String, Object>> selectProjectByDeviceType(String userId, String deviceTypeId, String projectName) {
        return appTabDeviceMapper.selectProjectByDeviceType(userId, deviceTypeId, projectName);
    }
    @Override
    public Integer selectProjectByDeviceTypeCount(String userId, String deviceTypeId, String projectName) {
        return appTabDeviceMapper.selectProjectByDeviceTypeCount(userId, deviceTypeId, projectName);
    }
    @Override
    public List<Map<String, Object>> projectByDeviceTypeAndId(String deviceTypeId, String projectId) {
        return appTabDeviceMapper.projectByDeviceTypeAndId(deviceTypeId, projectId);
    }
    @Override
    public List<Map<String,Object>> monitorStatistics(String sensorType,String type,String sensorNo) {
        List<Map<String,Object>> result = new ArrayList<Map<String,Object>>();
        String startTime = null;
        String endTime = null;
        switch (type) {
            case "today":
                startTime = (String) DateFormatUtil.getToday().get("start_time");
                endTime = (String) DateFormatUtil.getToday().get("end_time");
                break;
            case "threeDay":
                startTime = (String) DateFormatUtil.getPreToday(-2).get("start_time");
                endTime = (String) DateFormatUtil.getPreToday(-2).get("end_time");
                break;
            case "week":
                startTime = (String) DateFormatUtil.getMonSumTime().get("mon");
                endTime = (String) DateFormatUtil.getMonSumTime().get("sun");
                break;
            case "month":
                startTime = (String) DateFormatUtil.getMonthFirstAndEndDay().get("first");
                endTime = (String) DateFormatUtil.getMonthFirstAndEndDay().get("end");
                break;
            case "3month":
                startTime = (String) DateFormatUtil.getPreThreeMonth(-2).get("first");
                endTime = (String) DateFormatUtil.getPreThreeMonth(-2).get("end");
                break;
        }
        SearchVo vo = new SearchVo();
        vo.setDateType(type);
        vo.setStartTime(startTime);
        vo.setEndTime(endTime);
        vo.setSensorNo(sensorNo);
        Integer sType = Integer.valueOf(sensorType);
        Map<String,Object> unitMap=appTabStaticTypeMapper.selectByStaticNumAndValue("1", sType.toString());
        String unit = unitMap.get("remark").toString();
        switch (sType) {
            case 10:
            	List<Map<String, Object>> tbsYl = appTabDeviceMapper.tbsYl(vo);
                result.add(parseData(tbsYl,"param1","雨量值","time",unit,sensorNo,"value"));
            	break;
        	case 22:
        		List<Map<String,Object>> sbwy = appTabDeviceMapper.tbsSbwy(vo);
	            result.add(parseData(sbwy,"param1","x角度","time", unit,sensorNo,"x"));
	            result.add(parseData(sbwy,"param2","y角度","time", unit,sensorNo,"y"));
	            result.add(parseData(sbwy,"param3","z角度","time", unit,sensorNo,"z"));
	            result.add(parseData(sbwy,"param4","x加速度","time",unit,sensorNo,"x1"));
	            result.add(parseData(sbwy,"param5","y加速度","time",unit,sensorNo,"y1"));
	            result.add(parseData(sbwy,"param6","z加速度","time",unit,sensorNo,"z1"));
	            result.add(parseData(sbwy,"param7","拉线值","time",unit,sensorNo,"lx"));
	            break;
        	case 32:
	            List<Map<String,Object>> gnss = appTabDeviceMapper.tbsGnss(vo);
	            result.add(parseData(gnss,"param1","x位移", "time",unit,sensorNo,"x"));
	            result.add(parseData(gnss,"param2","y位移", "time",unit,sensorNo,"y"));
	            result.add(parseData(gnss,"param3","h位移", "time",unit,sensorNo,"h"));
	            result.add(parseData(gnss,"param4","水平位移","time",unit,sensorNo,"s"));
	            result.add(parseData(gnss,"param5","三维位移","time",unit,sensorNo,"w"));
	            break;
        	case 53:
        		List<Map<String,Object>> tbsBmwy = appTabDeviceMapper.tbsBmwy(vo);
	            result.add(parseData(tbsBmwy,"param1","x角度","time", unit,sensorNo,"x"));
	            result.add(parseData(tbsBmwy,"param2","y角度","time", unit,sensorNo,"y"));
	            result.add(parseData(tbsBmwy,"param3","z角度","time", unit,sensorNo,"z"));
	            result.add(parseData(tbsBmwy,"param4","x加速度","time",unit,sensorNo,"x1"));
	            result.add(parseData(tbsBmwy,"param5","y加速度","time",unit,sensorNo,"y1"));
	            result.add(parseData(tbsBmwy,"param6","z加速度","time",unit,sensorNo,"z1"));
        		break;
            default : throw new RuntimeException("无该类型"+sType+"的设备");
        }
        return result;
    }

    /**
     * @return 功能描述：解析数据
     **/
    private Map<String,Object> parseData(List<Map<String,Object>> list,String itemKey,
    		String itemName,String dataKey,String unit,String sensorNo,String filedName){
        HashMap<String, Object> resultMap = new HashMap<String, Object>();
        List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
        Map<String, Object> initValueMap = tabSensorInitMapper.getDataByFiledName(sensorNo, filedName);
        list.forEach(item -> {
            HashMap<String, Object> map = new HashMap<String, Object>();
            BigDecimal initValue = BigDecimal.ZERO;
            if (null != initValueMap && null != initValueMap.get("init_value")) {
                initValue = new BigDecimal(initValueMap.get("init_value").toString());
            }
            map.put("x", dateFormat(item.get(dataKey).toString()));
            if (!StringUtils.isEmpty(item.get(itemKey))) {
                BigDecimal value = new BigDecimal(item.get(itemKey).toString()).subtract(initValue);
                map.put("y", value);
            }
            resultList.add(map);
        });
        resultMap.put("name", itemName);
        resultMap.put("type", itemKey);
        resultMap.put("data", resultList);
        resultMap.put("unit", unit);
        return resultMap;
    }

    private String dateFormat(String str) {
        return str.replace("Z","").replace("T", " ");
    }
   
}